package com.zhitong.money.mapper;

import com.zhitong.money.entity.Employee;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface EmployeeMapper {
    @Results(id = "employeeMap",value = {
            @Result(column = "id",property = "id"),
            @Result(column = "tellerId",property = "tellerId"),
            @Result(column = "idCard",property = "idCard"),
            @Result(column = "pid",property = "pid"),
            @Result(column = "name",property = "name"),
            @Result(column = "age",property = "age"),
            @Result(column = "sex",property = "sex"),
            @Result(column = "education",property = "education"),
            @Result(column = "birthday",property = "birthday"),
            @Result(column = "address",property = "address"),
            @Result(column = "phone",property = "phone"),
            @Result(column = "email",property = "email"),
            @Result(column = "family",property = "family"),
            @Result(column = "fPhone",property = "fPhone"),
            @Result(column = "uStatus",property = "uStatus"),
            @Result(column = "status",property = "status")
    })
    @Select("select * from employee")
    public List<Employee> findAllEmployee();

    @ResultMap("employeeMap")
    @Select("select * from employee where id = #{id}")
    public Employee findEmployeeById(String id);

    @ResultMap("employeeMap")
    @Select("select * from employee where tellerId = #{tellerId}")
    public Employee findEmployeeByTellerId(String tellerId);

    @ResultMap("employeeMap")
    @Select("select * from employee where pid = #{pid}")
    public List<Employee> findEmployeeByPid(String pid);

    @ResultMap("employeeMap")
    @Select({
            "<script>",
            "select * from employee",
            "where tellerId in",
            "<foreach item='item' collection='tids'  separator=',' open='(' close=')' index=''>",
            "#{item}",
            "</foreach>",
            "</script>"
    })
    public List<Employee> findEmployeeByTids(List<String> tids);

    @Select("select id from employee where pid = #{pid}")
    public List<String> findIdsByPid(String pid);

    @Select({
            "<script>",
            "select id from employee",
            "where pid in",
            "<foreach item='item' collection='pids'  separator=',' open='(' close=')' index=''>",
            "#{item}",
            "</foreach>",
            "</script>"
    })
    public List<String> findIdsByPids(List<String> pids);

    @Select({
            "<script>",
            "select tellerId from employee",
            "where pid in",
            "<foreach item='item' collection='pids'  separator=',' open='(' close=')' index=''>",
            "#{item}",
            "</foreach>",
            "</script>"
    })
    public List<String> findTellIdsByPids(List<String> pids);

    @ResultMap("employeeMap")
    @Select("select * from employee where idCard = #{idCard}")
    public Employee findEmployeeByIdCard(String idCard);

    @ResultMap("employeeMap")
    @Select({
            "<script>",
            "select * from employee",
            "<where>",
            "<if test='tellerId!=null and tellerId!=\"\"'>and tellerId like concat(concat('%',#{tellerId},'%'))</if>",
            "<if test='idCard!=null and idCard!=\"\"'>and idCard like concat(concat('%',#{idCard},'%'))</if>",
            "<if test='pid!=null and pid!=\"\"'>and pid=#{pid}</if>",
            "<if test='name!=null and name!=\"\"'> and name like concat(concat('%',#{name},'%'))</if>",
            "<if test='age!=null and age!=\"\"'>and age=#{age}</if>",
            "<if test='sex!=null and sex!=\"\"'>and sex=#{sex}</if>",
            "<if test='education!=null and education!=\"\"'>and education=#{education}</if>",
            "<if test='uStatus!=null and uStatus!=\"\"'>and uStatus=#{uStatus}</if>",
            "<if test='status!=null and status!=\"\"'>and status=#{status}</if>",
            "</where>",
            "</script>"
    })
    public List<Employee> findEmployeeByMap(Map<String,Object> map);


    @Insert("insert into employee values(#{id},#{tellerId},#{idCard}," +
            "#{pid},#{name},#{age},#{sex},#{education},#{birthday},#{address}," +
            "#{phone},#{email},#{family},#{fPhone},#{uStatus},#{status})")
    public void addEmployee(Employee employee);

    @Update({
            "<script>",
            "update employee",
            "<trim prefix='set' suffixOverrides=','>",
            "<if test='tellerId!=null and tellerId!=\"\"'> tellerId=#{tellerId},</if>",
            "<if test='idCard!=null and idCard!=\"\"'> idCard=#{idCard},</if>",
            "<if test='pid!=null and pid!=\"\"'> pid=#{pid},</if>",
            "<if test='name!=null and name!=\"\"'> name=#{name},</if>",
            "<if test='age!=null and age!=\"\"'>age=#{age},</if>",
            "<if test='sex!=null and sex!=\"\"'> sex=#{sex},</if>",
            "<if test='education!=null and education!=\"\"'> education=#{education},</if>",
            "<if test='birthday!=null'> birthday=#{birthday},</if>",
            "<if test='address!=null and address!=\"\"'> address=#{address},</if>",
            "<if test='phone!=null and phone!=\"\"'> phone=#{phone},</if>",
            "<if test='email!=null and email!=\"\"'> email=#{email},</if>",
            "<if test='family!=null and family!=\"\"'> family=#{family},</if>",
            "<if test='fPhone!=null and fPhone!=\"\"'> fPhone=#{fPhone},</if>",
            "<if test='uStatus!=null and uStatus!=\"\"'> uStatus=#{uStatus},</if>",
            "<if test='status!=null and status!=\"\"'> status=#{status},</if>",
            "</trim>",
            "where id = #{id}",
            "</script>"
    })
    public void updateEmployee(Employee employee);

    @Update("update employee set uStatus=#{ustatus} where tellerId =#{tid}")
    public void updateUStatusByTid(String tid,String ustatus);

    @Update({
            "<script>",
            "update employee",
            "set uStatus = #{uStatus}",
            "where id in",
            "<foreach item='item' collection='tids' separator=',' open='(' close=')' index=''>",
            "#{item}",
            "</foreach>",
            "</script>"
    })
    public void updateUStatusByTids(List<String> tids,String uStatus);

    @Update({
            "<script>",
            "update employee",
            "set pid = #{pid}",
            "where id in",
            "<foreach item='item' collection='ids' separator=',' open='(' close=')' index=''>",
            "#{item}",
            "</foreach>",
            "</script>"
    })
    public void updatePidByIds(List<String> ids,String pid);

    @Delete("delete from employee where id = #{id}  or tellerId=#{tellerId} or idCard=#{idCard}")
    public void delEmployee(String id);

    @Delete({
            "<script>",
            "delete from employee",
            "where id in",
            "<foreach item='item' collection='ids'  separator=',' open='(' close=')' index=''>",
            "#{item}",
            "</foreach>",
            "</script>"
    })
    public void delEmployeeByIds(List<String> ids);



}
